package com.petot.claude.gwt.example.server.service;

import java.util.ArrayList;
import java.util.List;

import com.google.web.bindery.requestfactory.shared.Locator;
import com.petot.claude.gwt.example.server.model.Address;
import com.petot.claude.gwt.example.server.model.Person;

public class PersonService extends Locator<Person,Long> {

    private static List<Person> persons;
    static {
        persons = new ArrayList<Person>();
        persons.add( new Person( 1L, "Maxime", "Pralong", 10, new Address( "avenue Lacassagne", "Lyon", 56, 69003 ) ) );
        persons.add( new Person( 2L, "Jessica", "Thiers", 36, new Address( "avenue Lacassagne", "Lyon", 56, 69003 ) ) );
        persons.add( new Person( 3L, "Pascal", "Pralong", 42, new Address( "rue Lançon", "Lyon", 14, 69100 ) ) );
    }

    @Override
    public Person create( Class<? extends Person> clazz ) {
        return new Person();
    }

    @Override
    public Person find( Class<? extends Person> clazz, Long id) {
       for (Person person : persons ) {
           if (person.getId() == id) {
               return person;
           }
       }
       return null;
    }

    public List<Person> findAll() {
        return persons;
    }

    public long count() {
        return persons.size();
    }

    public void create (Person person) {
        persons.add(person);
    }

    @Override
    public Class<Person> getDomainType() {
        return Person.class;
    }

    @Override
    public Long getId( Person person ) {
        return person.getId();
    }

    @Override
    public Class<Long> getIdType() {
        return Long.class;
    }

    @Override
    public Object getVersion( Person pArg0 ) {
        return 1; // Not managed for this example
    }

}
